
import java.awt.Point;
import java.util.Random;

public class Wander implements State {

    private FSMachine m_fsm;
    private Point m_desPos;
    private int m_nID;
    private int turnsLeft;
    private boolean primaryDirection;
    private boolean primarySign;

    public Wander(FSMachine fsm) {
        m_fsm = fsm;
    }

    public void Enter(int myID, int otherID) {
        m_nID = myID;
        //System.out.println("Agent " + m_nID + "  Wander (enter)");
        turnsLeft = 10;
        Random r = new Random();
        primaryDirection = r.nextBoolean();
        primarySign = r.nextBoolean();
        Global.agents.get(myID).isBusy = true;
    }

    public void Execute() {
        // move to a random place
        
       // System.out.println("Agent " + m_nID + " Wander (execute)");
        //System.out.println("initial locx="+Global.agents.get(m_nID).locx+" initial locy="+Global.agents.get(m_nID).locy);

        if (turnsLeft > 0) {
            double theta = new Random().nextDouble() * 2.0 * Math.PI;

            double deltaX = Global.WALKING_SPEED * Math.cos(theta);
            double deltaY = Global.WALKING_SPEED * Math.sin(theta);
            if (primaryDirection) {
            	deltaX = Math.abs(deltaX) * (primarySign ? 1 : 0) * 2.5;
            	deltaY *= .8;
            } else {
            	deltaY = Math.abs(deltaY) * (primarySign ? 1 : 0) * 2.5;
            	deltaX *= .8;
            }
            
            Global.agents.get(m_nID).locx += deltaX;
            Global.agents.get(m_nID).locy += deltaY;
            
            //System.out.println("locx="+Global.agents.get(m_nID).locx+" locy="+Global.agents.get(m_nID).locy);
            turnsLeft--;
        } else {
            m_fsm.ChangeState(FSMachine.enumState.GUESS, m_nID, -1);
            //System.out.println("final locx="+Global.agents.get(m_nID).locx+" final locy="+Global.agents.get(m_nID).locy);
            //System.out.println("Agent " + m_nID + " Wander (execute)");
            return;
        }
    }

    public void Exit() {
        System.out.println("Agent "+m_nID+" wandered for a little bit");
    	Global.agents.get(m_nID).isBusy = false;
    }
}
